import {config } from "./config"

function getHeroInfo(obj,type){
    var info={data:{x:obj.x,y:obj.y,name:obj.name},type:type}
    return JSON.stringify(info);
}



function save() {
    var hero_data = {};
    hero_data.life = hero.life;
    hero_data.bulletType = hero.bulletType;
    hero_data.type = hero.type;
    hero_data.score = score;
    hero_data.level = level;
    var string_data = JSON.stringify(hero_data);
    localStorage.setItem("hero_data", string_data);
    alert('保存成功')
}


var LastBoomSound = 0
export function playBoomSound() {
    var now = new Date().getTime();
    if (now - LastBoomSound > 2000) {
        Sounds.bossBz.play();
        LastBoomSound = now;
    } else {
        const audio3 = new Audio("song/bossBz.mp3");
        audio3.play()
    }
}

function text() {
    ctx.fillStyle = "red";
    ctx.font = "60px '微软雅黑'";
    ctx.textAlign = "center";
    //shadowBlur:模式级数
    //  ctx.shadowBlur = 10;
    //  ctx.shadowOffsetX = 5;
    //  ctx.shadowOffsetY = 5;
    //  ctx.shadowColor = "black";
    //fillText("要添加的文字",x0坐标，y0坐标)
    // ctx.strokeText("You jump! I jump!",50,100);
    ctx.fillText("继续", canvas_width / 2, 100);
    ctx.fillText("保存", canvas_width / 2, 200);
}

// 获取两个矩形相交区域的中心点与objB原点的x,y距离
export function getHitPoint(objA, objB){
    var res = {
        x:0, y:0
    }
    var objA_Left = objA.x
    var objA_Right = objA.x+objA.img.width
    var objB_Left = objB.x
    var objB_Right = objB.x+objB.img.width

    console.log(objA_Left, objA_Right, objB_Left, objB_Right)

    if (objA_Left <= objB_Left && objA_Right >= objB_Left) {
        if (objA_Right < objB_Right ) {
            res.x = (objA_Right - objB_Left) / 2
        } else {
            res.x = (objB_Right - objB_Left) / 2
        }
    }
    else if (objA_Left >= objB_Left && objA_Left <= objB_Right) {
        if (objA_Right <= objB_Right) {
            res.x = objA_Left - objB_Left + (objA_Right - objA_Left) / 2
        } else {
            res.x = objA_Left - objB_Left + (objB_Right - objA_Left) / 2
        }
    }

    var objA_Up = objA.y
    var objA_Bottom = objA.y+objA.img.height
    var objB_Up = objB.y
    var objB_Bottom = objB.y+objB.img.height
    
    if (objA_Up <= objB_Up && objA_Bottom >= objB_Up) {
        if (objA_Bottom < objB_Bottom ) {
            res.y = (objA_Bottom - objB_Up) / 2
        } else {
            res.y = (objB_Bottom - objB_Up) / 2
        }
    }
    else if (objA_Up >= objB_Up && objA_Up <= objB_Bottom) {
        if (objA_Bottom <= objB_Bottom) {
            res.y = objA_Up - objB_Up + (objA_Bottom - objA_Up) / 2
        } else {
            res.y = objA_Up - objB_Up + (objB_Bottom - objA_Up) / 2
        }
    }
    
    return res
}

export function paintLife(obj){
    var paintw=obj.width*(obj.life/obj.total_life)
    if (paintw <= 0 ) {
        return
    }
    
    ctx.beginPath();
    ctx.moveTo (obj.x,obj.y-2);      
    ctx.lineTo (obj.x+obj.width,obj.y-2);       
    ctx.lineWidth = 5;          
    ctx.strokeStyle ="blue";  
    ctx.stroke();   
    ctx.closePath();
    
    ctx.beginPath();
    ctx.moveTo (obj.x,obj.y-2);      
    ctx.lineTo (obj.x+paintw,obj.y-2);       
    ctx.lineWidth = 5;          
    ctx.strokeStyle ="red";  
    ctx.stroke();   
    ctx.closePath();
}

export function getlev() {
    return
    let score = window.globalData.score
    if(score < 20 && score <=200){
        window.globalData.level = 1;
        config.makeEnemyInterval =3000;
    }else if (score > 200 && score < 500) {
     
        window.globalData.level = 2;
        config.makeEnemyInterval = 1700;
    } else if (score > 500 && score < 700) {
        window.globalData.level = 3;
        config.makeEnemyInterval = 1500;
    } else if (score > 700 && score < 1000) {
        window.globalData.level = 4;
        config.makeEnemyInterval = 1200;
    } else if (score > 1000 && score < 1200) {
        window.globalData.level = 5;
        config.makeEnemyInterval = 1000;
    } else if (score > 1200 && score < 1500) {
        window.globalData.level = 6;
        config.makeEnemyInterval = 900;
    } else if (score > 1500 && score < 1700) {
        window.globalData.window.globalData.level = 7;
        config.makeEnemyInterval = 700;
    } else if (score > 1700 && score < 2000) {
        window.globalData.level = 8;
        config.makeEnemyInterval = 500;
    } else if (score > 2000 && score < 3000) {
        window.globalData.level = 9;
        config.makeEnemyInterval = 400;
    } else if (score > 3000) {
        window.globalData.level = 10;
        config.makeEnemyInterval = 200;
    }
}

